$(function () {
    $('#releaseTimeBeginId,#releaseTimeEndId').datetimepicker({
        lang:'ch',format:'Y-m-d','timepicker': false,'datepicker':true,
        onSelectDate: function(dateText, inst) {
            this.hide();
        }});

    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/song/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '曲谱名称', name: 'name', index: 'name', width: 80 },
            { label: '曲谱难度', name: 'hardName', index: 'hard', width: 80 },
            { label: '曲谱类型', name: 'typeName', index: 'type', width: 80 },
            { label: '作者', name: 'author', index: 'author', width: 80 },
            { label: '上传者', name: 'uploadUser', index: 'upload_user', width: 80 },
			{ label: '上架时间', name: 'releaseTime', index: 'release_time', width: 80 }, 			
			{ label: '曲谱', name: 'opern', index: 'opern', width: 80 ,hidden: true},
			{ label: '', name: 'createTime', index: 'create_time', width: 80 ,hidden: true},
			{ label: '', name: 'updateTime', index: 'update_time', width: 80 ,hidden: true}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    // 上传xml文件
    $("#opern").fileinput({
        language: 'zh',
        showUpload: true,
        showCaption: false,
        dropZoneEnabled: false,
        showRemove:false,
        allowedFileExtensions : ['xml'],
        maxFileCount:1,//上传图片最大数量
        uploadUrl: baseURL+"sys/file/uploadFile",
        browseClass: "btn btn-primary",
        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
        layoutTemplates:{
            actionDelete:'',
            actionZoom:'',
            actionUpload:''
        }
    }).on("fileuploaded", function (e, data) {
        var res = data.response;
        if (res.code == 0) {
            alert('上传成功');
            vm.song.opern=res.path;
            vm.song.opernName=res.fileName;
            //  alert(res.path);
        }
        else {
            alert('上传失败')
        }
    });

    // 上传图片
    $("#img").fileinput({
        language: 'zh',
        showUpload: true,
        showCaption: false,
        dropZoneEnabled: false,
        showRemove:false,
        allowedFileExtensions : ['jpg', 'png','gif'],
        maxFileCount:1,//上传图片最大数量
        uploadUrl: baseURL+"sys/file/uploadFile",
        browseClass: "btn btn-primary",
        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
        layoutTemplates:{
            actionDelete:'',
            actionZoom:'',
            actionUpload:''
        }
    }).on("fileuploaded", function (e, data) {
        var res = data.response;
        if (res.code == 0) {
            alert('上传成功');
            vm.song.img=res.path;
            // vm.song.opernName=res.fileName;
            //  alert(res.path);
        }
        else {
            alert('上传失败')
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		song: {},
        q:{
		    name:null,
            author:null,
            uploadUser:null,
            releaseTimeBegin:null,
            releaseTimeEnd:null
        }
	},
	methods: {
		query: function () {
            vm.q.releaseTimeBegin=$('#releaseTimeBeginId').val();
            vm.q.releaseTimeEnd=$('#releaseTimeEndId').val();
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.song = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            if(vm.song.name==null||vm.song.name==''){
                alert("请填写名称");
                return ;
            }
            if(vm.song.introduce==null||vm.song.introduce==''){
                alert("请填写简介");
                return ;
            }
            if(vm.song.img==null||vm.song.img==''){
                alert("请上传图片");
                return ;
            }
            if(vm.song.hard==null){
                alert("请选择难度");
                return ;
            }
            if(vm.song.type==null){
                alert("请选择类型");
                return ;
            }
            if(vm.song.author==null||vm.song.author==''){
                alert("请填写作者");
                return ;
            }
            if(vm.song.uploadUser==null||vm.song.uploadUser==''){
                alert("请填写上传者");
                return ;
            }
            if(vm.song.opern==null||vm.song.oper==''){
                alert("请上传曲谱");
                return ;
            }
			var url = vm.song.id == null ? "sys/song/save" : "sys/song/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.song),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/song/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/song/info/"+id, function(r){
                vm.song = r.song;
                $("#img").fileinput('refresh',{
                    initialPreview: '<img width="250px" height="150px" src="'+baseURL+"sys/file/getImg?path="+vm.song.img+'" class="file-preview-image">'
                });
            });
		},
		reload: function (event) {
            $("#img").fileinput('clear');
            $("#opern").fileinput('clear');
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
			    postData:{
			        name:vm.q.name,
                    author:vm.q.author,
                    uploadUser:vm.q.uploadUser,
                    releaseTimeBegin:vm.q.releaseTimeBegin,
                    releaseTimeEnd:vm.q.releaseTimeEnd
                },
                page:page
            }).trigger("reloadGrid");
		},
        downloadFile: function (path) {
		    window.open(baseURL+"sys/file/downloadFile?path="+path);
        }
	}
});